#导包
import datetime
import os
import time


class BasePage:
    def __init__(self,driver,url):
        self.driver = driver
        self.url = url
    #进入网址
    def get(self):
        self.driver.get(self.url)
    #点击
    def click(self,locator):
        self.driver.find_element(*locator).click()
    #输入
    def input(self,locator,text):
        self.driver.find_element(*locator).send_keys(text)
    #获取文本
    def get_text(self,locator):
        return self.driver.find_element(*locator).text
    #iframe页面切换
    def switch_to_frame(self,locator):
        self.driver.switch_to.frame(self.driver.find_element(*locator))
        self.driver.switch_to.default_content()
    #窗口切换
    def switch_to_window(self):
        handles = self.driver.window_handle
        self.driver.switch_to.window(handles[-1])
    #截图
    def screenshot(self, name_prefix):
        now = time.strftime("%Y-%m-%d %H_%M_%S")
        dirpath = './screenshot'
        os.makedirs(dirpath, exist_ok=True)
        reportName = dirpath + '/' + name_prefix + now + '.png'
        with open(reportName, 'wb') as f:
            self.driver.get_screenshot_as_png()